#pragma once
#include"event/Object.h"
#include"DBMaster.h"
#include"DecodeMessageBuffer.h"
#include"EncodeMessageBuffer.h"
#include"gameLogic/GameScene.h"
#include<map>
#include<string>
#include<vector>

class GameServer;

class ServerMaster:public Object
{
friend class GameServer;
public:
	ServerMaster();
	ServerMaster(GameServer* server);
	~ServerMaster();
	DBMaster* getDBMaster();
	GameServer* getGameServer();
	void decodeMessage(char* buff,const int& len, Json::Value* out);
	EncodeBuff encodeMessage(Json::Value& message);
	pbc_env* getPbcEnv();

	template<typename T>
	T* getModule();
	
	SceneBase* getGameScene(const int& sceneId);
private:
	void registerModel();
	void initModel();
	void initPbcEnv();
	void registerGameScene();
private:
	GameServer* m_server;
	DecodeMessageBuffer m_decodeBuffer;
	EncodeMessageBuffer m_encodeBuffer;
	struct pbc_env* m_env = nullptr;
	DBMaster* m_dbMaster;

	std::map<std::string, Object*> m_moduleMaps;
	std::vector<SceneBase*> m_gameSceneVec;
};

template<typename T>
inline T* ServerMaster::getModule()
{
	return dynamic_cast<T*>(m_moduleMaps[typeid(T).name()]);
}
